#include "sdk.h"
#include "se_globals.h"

/*取父目录*/
wxString GetParentDir(const wxString &path)
{
    wxString parent=wxFileName(parent).GetPath(0);
    if (path == parent || parent.IsEmpty())
        return wxEmptyString;
    else
        return parent;
}

/*目录是指定目录的子目录*/
bool DirIsChildOf(const wxString &path, const wxString &/*child*/)
{
    wxString parent=path;
    while(!parent.IsEmpty())
    {
        if (wxFileName(path).SameAs(parent)) { return true; }
        parent=GetParentDir(parent);
    }
    return false;
}

/*通配符列表匹配*/
bool WildCardListMatch(wxString list, wxString name, bool strip)
{
    /*默认情况下,任何空列表都匹配所有内容*/
    if (list == _T("")) { return true; }

    wxString wildlist=list;
    wxString wild=list.BeforeFirst(';');
    if (strip) { wild = wild.Strip(wxString::both); }

    while(wildlist!=_T(""))
    {
        if (wild != _T("") && ::wxMatchWild(wild, name)) { return true; }

        wildlist=wildlist.AfterFirst(';');
        wild=wildlist.BeforeFirst(';');
        if (strip) { wild = wild.Strip(wxString::both); }
    }
    return false;
}

/*保存/打开文件提示*/
bool PromptSaveOpenFile(wxString message, wxFileName path)
{
    EditorManager* em = Manager::Get()->GetEditorManager();
    EditorBase *eb=em->IsOpen(path.GetFullPath());
    if(eb)
    {
        if(eb->GetModified())
            switch(cbMessageBox(message,wxT("保存文件?"),wxYES_NO|wxCANCEL))
            {
                case wxYES:
                    if(!eb->Save())
                        cbMessageBox(wxT("保存失败 - 继续处理未保存的文件"));
                case wxNO:
                    eb->Close();
                    return true;
                case wxCANCEL:
                    return false;
                default:
                    break;
            }
    }
    return true;
}
